import java.io.InputStream;
import java.nio.charset.Charset;
import java.sql.*;
import java.util.Properties;

public class TestQuery {

    public static void main(String[] args) {
        try {
            ClassLoader classLoader = TestQuery.class.getClassLoader();
            try (InputStream is = classLoader.getResourceAsStream("config.properties")) {
                Properties properties = new Properties();
                properties.load(is);
                String charset = properties.getProperty("charset");
                String cname = properties.getProperty("driver");
                String url = properties.getProperty("url");
                String username = properties.getProperty("username");
                String passwd = properties.getProperty("password");
                String selectSql = properties.getProperty("select_sql");

                Class.forName(cname);
                try (Connection conn = DriverManager.getConnection(url, username, passwd);
                     Statement stmt = conn.createStatement();
                     ResultSet rs = stmt.executeQuery(selectSql)) {

                    ResultSetMetaData metaData = rs.getMetaData();
                    int columnCount = metaData.getColumnCount();
                    System.out.println( "字段类型:Java类型:字段名值");
                    while (rs.next()) {
                        for (int i = 0; i < columnCount; i++) {
                            String columnName = metaData.getColumnName(i + 1);
                            String columnTypeName = metaData.getColumnTypeName(i + 1);
                            Object value = rs.getObject(i + 1);
                            if (value instanceof Blob) {
                                final InputStream in = ((Blob) value).getBinaryStream();
                                byte[] data = new byte[in.available()];
                                in.read(data);
                                value = new String(data, Charset.forName(charset));
                            }
                            System.out.println(columnTypeName + ":" + value.getClass().getSimpleName() + ":" + columnName + ":" + value);
                            System.out.println("==========================================");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
